#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using QueryBuilder;

#endregion

namespace TVC.Forms
{
    public partial class frmLKC : Form
    {
        string _strType = "LKC";
        string _Category = "";
        DataRow _returnValue = null;
        string sErr = "";
        public MainForm MainForm { get; set; }
        public DataRow ReturnValue
        {
            get { return _returnValue; }
            set { _returnValue = value; }
        }
        public frmLKC(string cate)
        {
            InitializeComponent();
            _Category = cate;
        }

        private void frmPOG_Load(object sender, EventArgs e)
        {
            dgvList.AutoGenerateColumns = false;
            BUS.CategoryControl cateCtr = new BUS.CategoryControl();
            DTO.CategoryInfo cateinfo = cateCtr.Get(MainForm.DB, _Category, ref sErr);
            if (cateinfo.QdCode == "")
            {
                if (!MainForm.IsContainsTables(_strType))
                    MainForm.LoadDataTable(_strType);
                Rebind();
            }
            else
            {
                DataTable dt = MainForm.LoadQDSource(MainForm.DB, cateinfo);
                bisLKC.DataSource = dt;
            }
        }

        private DataTable LoadQDSource(DTO.CategoryInfo cateinfo)
        {
            SQLBuilder sqlBuilder = SQLBuilder.LoadSQLBuilderFromDataBase(cateinfo.QdCode, MainForm.DB, "");
            DataTable dt = sqlBuilder.BuildDataTable("");
            foreach (Node node in sqlBuilder.SelectedNodes)
            {
                if (node.Code == cateinfo.NodeCode)
                {
                    if (dt.Columns.Contains(node.Description))
                    {
                        dt.Columns[node.Description].ColumnName = "Code";
                    }
                }
                if (node.Code == cateinfo.NodeDesc)
                {
                    if (dt.Columns.Contains(node.Description))
                    {
                        dt.Columns[node.Description].ColumnName = "Description";
                    }
                }
                if (node.Code == cateinfo.NodeLookup)
                {
                    if (dt.Columns.Contains(node.Description))
                    {
                        dt.Columns[node.Description].ColumnName = "Lookup";
                    }
                }
                if (node.Code == cateinfo.NodeStatus)
                {
                    if (dt.Columns.Contains(node.Description))
                    {
                        dt.Columns[node.Description].ColumnName = "Status";
                    }
                }
            }
            try
            {
                dt.PrimaryKey = new DataColumn[] { dt.Columns["Code"] };
            }
            catch { }
            return dt;
        }

        private void Rebind()
        {
            DataRow[] arrRow = MainForm.GetTable(_strType).Select("Category = '" + _Category + "'");
            DataTable dt = MainForm.GetTable(_strType).Clone();
            foreach (DataRow row in arrRow)
            {
                dt.LoadDataRow(row.ItemArray, true);
            }
            bisLKC.DataSource = dt;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            BUS.CategoryControl cateCtr = new BUS.CategoryControl();
            DTO.CategoryInfo cateinfo = cateCtr.Get(MainForm.DB, _Category, ref sErr);
            if (cateinfo.QdCode == "")
            {
                MainForm.LoadDataTable(_strType);
                Rebind();
            }
            else
            {
                DataTable dt = LoadQDSource(cateinfo);
                bisLKC.DataSource = dt;
            }
        }

        private void dgvList_DoubleClick(object sender, EventArgs e)
        {
            if (dgvList.CurrentCell != null)
            {
                _returnValue = ((DataRowView)dgvList.CurrentRow.DataBoundItem).Row;
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentCell != null)
            {
                _returnValue = ((DataRowView)dgvList.CurrentRow.DataBoundItem).Row;
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}